
package com.ptppay.cf.coin.service.btc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.bitcoinj.wallet.Wallet;

import cn.finder.bitcoin.Btc;
import cn.finder.bitcoin.BtcTools;
import cn.finder.ui.webtool.QueryCondition;
import cn.finder.wae.business.domain.Message;
import cn.finder.wae.business.domain.TableQueryResult;
import cn.finder.wae.business.dto.MapParaQueryConditionDto;
import cn.finder.wae.queryer.handleclass.QueryerDBAfterClass;

/**
 * @author: finder
 * @function:钱包余额查询 coin
 */
public class CreateEmptyWalletAfterClass extends QueryerDBAfterClass {

	private Logger logger = Logger.getLogger(CreateEmptyWalletAfterClass.class);

	@Override
	public TableQueryResult handle(TableQueryResult tableQueryResult, long showTableConfigId,
			QueryCondition<Object[]> condition) {
		super.handle(tableQueryResult, showTableConfigId, condition);
		logger.info("===" + this.getClass());
		@SuppressWarnings("unchecked")
		Map<String, Object> data = ((MapParaQueryConditionDto<String, Object>) condition).getMapParas();

		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();

		Map<String, Object> mapItem = new HashMap<String, Object>();
		tableQueryResult.setResultList(resultList);

		tableQueryResult.setPageIndex(condition.getPageIndex());
		tableQueryResult.setPageSize(condition.getPageSize());
		tableQueryResult.setCount(1l);

		resultList.add(mapItem);

		String coin_type = "BTC";


		if (tableQueryResult.getMessage().getResultCode() == Message.StatusCode_OK) {
			try {

				Wallet wallet = BtcTools.createEmptyWallet(Btc.get().getNetworkParameters());

				String seeds = BtcTools.createKeyChainSeedText(wallet);

				mapItem.put("seeds", seeds);

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				tableQueryResult.getMessage().setResultCode(Message.StatusCode_ServerError);
				tableQueryResult.getMessage().setResultMsg("余额查询失败");
			}

		}

		return tableQueryResult;

	}

	private TableQueryResult errorReturn(TableQueryResult tableQueryResult, int resultCode, String msg) {
		tableQueryResult.getMessage().setResultCode(resultCode);
		tableQueryResult.getMessage().setResultMsg(msg);
		tableQueryResult.getMessage().setMsg(msg);
		return tableQueryResult;
	}

	@Override
	public void setJDBCDataSource(DataSource dataSource) {
		super.setJdbcDataSource(dataSource);
	}

}
